package com.shike.palmrejection;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created by snoopy on 2018/1/21.
 */

public class FileManager {
    /**
     * 获取指定文件夹内所有文件大小的和
     *
     * @param file file
     * @return size
     * @throws Exception
     */
    public static long getFolderSize(File file){
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (File aFileList : fileList) {
                if (aFileList.isDirectory()) {
                    size = size + getFolderSize(aFileList);
                } else {
                    size = size + aFileList.length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }


    /**
     * 删除指定目录下的文件，这里用于缓存的删除
     *
     * @param filePath filePath
     * @param deleteThisPath deleteThisPath
     */
    public static void deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!TextUtils.isEmpty(filePath)) {
            try {
                File file = new File(filePath);
                if (file.isDirectory()) {
                    File files[] = file.listFiles();
                    for (File file1 : files) {
                        deleteFolderFile(file1.getAbsolutePath(), true);
                    }
                }
                if (deleteThisPath) {
                    if (!file.isDirectory()) {
                        file.delete();
                    } else {
                        if (file.listFiles().length == 0) {
                            file.delete();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeFile(Context mContext, String msg) {
//        try {
//            File writename = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) + "/msg.txt"); // 相对路径，如果没有则要建立一个新的output。txt文件
//            if (!writename.exists()) {
//                writename.createNewFile(); // 创建新文件
//            }
//
//            RandomAccessFile raf = new RandomAccessFile(writename,"rw");
//            raf.seek(writename.length());
//            raf.write(msg.getBytes());
//            raf.write("\n".getBytes());
//            raf.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }

//    public static void writeFile(String file,String msg) {
//        try {
//
//            File writename = new File(Environment.getExternalStorageDirectory().toString() + "/xk/" + file + ".text"); // 相对路径，如果没有则要建立一个新的output。txt文件
//            if (!writename.exists()) {
//                writename.createNewFile(); // 创建新文件
//            }
//
//            RandomAccessFile raf = new RandomAccessFile(writename,"rw");
//            raf.seek(writename.length());
//            raf.write(msg.getBytes());
//            raf.write("\n".getBytes());
//            raf.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }

}
